package com.changgou.order.controller;

import com.changgou.order.pojo.OrderItem;
import com.changgou.order.service.CartService;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * @Author: wzw
 * @Description 购物车模块
 * @Date: 2020/12/23 21:50
 * @version: 1.8
 */
@RestController
@RequestMapping("/cart")
@CrossOrigin
public class CartController {

    //注入serivce
    @Autowired
    private CartService cartService;
    
    /**
     * @author wzw
     * 商品加入购物车==>存在Redis中
     * @Date 21:54 2020/12/23
     * @param sku_id 商品id
     * @param num 数量
     * @return entity.Result
    **/
    @RequestMapping("/add")
    public Result add(Long sku_id,Integer num) {
        //假数据:Redis key = 用户名
        String username = "wzw";
        //实现功能:商品加入购物车==>存在Redis中
        cartService.add(sku_id, num, username);
        //返回值
        return new Result(true, StatusCode.OK, "添加购物车成功");
    }

    /**
     * @author wzw
     * 读取用户购物车列表
     * @Date 15:10 2020/12/24
     * @param
     * @return entity.Result
    **/
    @RequestMapping("/list")
    public Result list(){

        //假数据:Redis key = 用户名
        String username = "wzw";
        //实现功能:根据用户账号查询Redis中对应的购物车列表
        List<OrderItem> orderItemList = cartService.list(username);
        //返回值
        return new Result(true, StatusCode.OK, "查询购物车列表成功",orderItemList);

    }

}
